<?php
/**
 * 购买行为
 *
 *
 *
 *
 * by abc  www.abc.com 开发
 */
defined('InShopNC') or exit('Access Invalid!');

class exchangeLogic
{

    /**
     * 会员信息
     *
     * @var array
     */
    private $_member_info = array();

    /**
     * 下单数据
     *
     * @var array
     */
    private $_order_data = array();

    /**
     * 表单数据
     *
     * @var array
     */
    private $_post_data = array();

    /**
     * buy_1.logic 对象
     *
     * @var obj
     */
    private $_logic_buy_1;

    public function __construct()
    {
        $this->_logic_buy_1 = Logic('buy_1');
    }

    /**
     * 购买第一步
     *
     * @param unknown $cart_id            
     * @param unknown $ifcart            
     * @param unknown $member_id            
     * @param unknown $store_id            
     * @return Ambigous <multitype:unknown, multitype:unknown >
     */
    public function buyStep1($gift_id, $member_id)
    {
        
        // 得到购买商品信息
        $result = $this->getGiftList($gift_id);
        
        if (! $result['state']) {
            return $result;
        }
        
        // 得到页面所需要数据：收货地址、发票、代金券、预存款、商品列表等信息
        $result = $this->getBuyStep1Data($result['data'], $member_id);
        return $result;
    }

    /**
     * 第一步：处理购物车
     *
     * @param array $cart_id
     *            购物车
     * @param int $member_id
     *            会员编号
     */
    public function getGiftList($gift_id)
    {
        $model_gift = Model('lottery_gift');
        
        if (empty($gift_id)) {
            return callback(false, '所购商品无效');
        }
        
        if (count($gift_id) > 50) {
            return callback(false, '一次最多只可购买50种商品');
        }
        
        // 购物车列表
        $condition['lottery_gift_id'] = array('in', $gift_id);
        $gift_list = $model_gift->getGiftExtendList($condition);
        
        // [得到最新商品属性及促销信息]
        $gift_list = $this->_logic_buy_1->getGoodsGiftList($gift_list);
        
        // 商品列表 [优惠套装子商品与普通商品同级罗列]
        $goods_list = $this->_getGoodsList($gift_list);
        
        // 以店铺下标归类
        $store_cart_list = $this->_getStoreCartList($gift_list);
        
        $is_pick = true;
        foreach ($goods_list as $goods) {
            if(intval($goods['is_pick']) <= 0) {
                $is_pick = false;
                break;
            }
        }
        
        return callback(true, '', array(
            'goods_list' => $goods_list,
            'store_cart_list' => $store_cart_list, 
            'is_pick' => $is_pick
        ));
    }

    /**
     * 购买第一步：返回商品、促销、地址、发票等信息，然后交前台抛出
     *
     * @param unknown $member_id            
     * @param unknown $data
     *            商品信息
     * @return
     *
     */
    public function getBuyStep1Data($data, $member_id)
    {
        list ($goods_list, $store_cart_list) = $data;
        $goods_list = $data['goods_list'];
        $store_cart_list = $data['store_cart_list'];
        $is_pick = $data['is_pick'];
        
        // 定义返回数组
        $result = array();
        $result['is_pick'] = $is_pick;
        
        // 商品金额计算(分别对每个商品/优惠套装小计、每个店铺小计)
        list ($store_cart_list, $store_goods_total) = $this->_logic_buy_1->calcCartList($store_cart_list);
        $result['store_cart_list'] = $store_cart_list;
        $result['store_goods_total'] = $store_goods_total;
        
        // 取得店铺优惠 - 满即送(赠品列表，店铺满送规则列表)
        list ($store_premiums_list, $store_mansong_rule_list) = $this->_logic_buy_1->getMansongRuleCartListByTotal($store_goods_total);
        $result['store_premiums_list'] = $store_premiums_list;
        $result['store_mansong_rule_list'] = $store_mansong_rule_list;
        
        // 重新计算优惠后(满即送)的店铺实际商品总金额
        $store_goods_total = $this->_logic_buy_1->reCalcGoodsTotal($store_goods_total, $store_mansong_rule_list, 'mansong');
        
        $result['store_goods_total'] = $store_goods_total;
        // 返回店铺可用的代金券
        $store_voucher_list = $this->_logic_buy_1->getStoreAvailableVoucherList($store_goods_total, $member_id);
        $result['store_voucher_list'] = $store_voucher_list;
        
        // 返回需要计算运费的店铺ID数组 和 不需要计算运费(满免运费活动的)店铺ID及描述
        list ($need_calc_sid_list, $cancel_calc_sid_list) = $this->_logic_buy_1->getStoreFreightDescList($store_goods_total);
        $result['need_calc_sid_list'] = $need_calc_sid_list;
        $result['cancel_calc_sid_list'] = $cancel_calc_sid_list;
        
        // 将商品ID、数量、运费模板、运费序列化，加密，输出到模板，选择地区AJAX计算运费时作为参数使用
        $freight_list = $this->_logic_buy_1->getStoreFreightList($goods_list, array_keys($cancel_calc_sid_list));
        
        $result['freight_list'] = $this->buyEncrypt($freight_list, $member_id);
        
        // 输出用户默认收货地址
        $result['address_info'] = Model('address')->getDefaultAddressInfo(array(
            'member_id' => $member_id
        ));
        
        // 计算总运费 陈雨楠 2016-9-23
        $store_freight_list = $this->_logic_buy_1->calcStoreFreight( $freight_list, $result['address_info']['city_id']);
        // $freight11 = array();
        // $freight11['state'] = empty($store_freight_list) ? 'fail' : 'success';
        $result['freight_total'] = $store_freight_list;
        // echo json_encode($freight11);die;
        // 计算总运费 陈雨楠 2016-9-23 end
        
        // 输出有货到付款时，在线支付和货到付款及每种支付下商品数量和详细列表
        $pay_goods_list = $this->_logic_buy_1->getOfflineGoodsPay($goods_list);
        if (! empty($pay_goods_list['offline'])) {
            $result['pay_goods_list'] = $pay_goods_list;
            $result['ifshow_offpay'] = true;
        } else {
            // 如果所购商品只支持线上支付，支付方式不允许修改
            $result['deny_edit_payment'] = true;
        }
        
        // 发票 :只有所有商品都支持增值税发票才提供增值税发票
        foreach ($goods_list as $goods) {
            if (! intval($goods['goods_vat'])) {
                $vat_deny = true;
                break;
            }
        }
        // 不提供增值税发票时抛出true(模板使用)
        $result['vat_deny'] = $vat_deny;
        $result['vat_hash'] = $this->buyEncrypt($result['vat_deny'] ? 'deny_vat' : 'allow_vat', $member_id);
        
        // 输出默认使用的发票信息
        $inv_info = Model('invoice')->getDefaultInvInfo(array(
            'member_id' => $member_id
        ));
        if ($inv_info['inv_state'] == '2' && ! $vat_deny) {
            $inv_info['content'] = '增值税发票 ' . $inv_info['inv_company'] . ' ' . $inv_info['inv_code'] . ' ' . $inv_info['inv_reg_addr'];
        } elseif ($inv_info['inv_state'] == '2' && $vat_deny) {
            $inv_info = array();
            $inv_info['content'] = '不需要发票';
        } elseif (! empty($inv_info)) {
            $inv_info['content'] = '普通发票 ' . $inv_info['inv_title'] . ' ' . $inv_info['inv_content'];
        } else {
            $inv_info = array();
            $inv_info['content'] = '不需要发票';
        }
        $result['inv_info'] = $inv_info;
        
        $buyer_info = Model('member')->getMemberInfoByID($member_id);
        if (floatval($buyer_info['available_predeposit']) > 0) {
            $result['available_predeposit'] = $buyer_info['available_predeposit'];
        }
        if (floatval($buyer_info['available_rc_balance']) > 0) {
            $result['available_rc_balance'] = $buyer_info['available_rc_balance'];
        }
        $result['member_paypwd'] = $buyer_info['member_paypwd'] ? true : false;
        
        return callback(true, '', $result);
    }

    /**
     * 购买第二步
     *
     * @param array $post            
     * @param int $member_id            
     * @param string $member_name            
     * @param string $member_email            
     * @return array
     */
    public function buyStep2($post, $member_id, $member_name, $member_email)
    {
        $this->_member_info['member_id'] = $member_id;
        $this->_member_info['member_name'] = $member_name;
        $this->_member_info['member_email'] = $member_email;
        $this->_post_data = $post;
        
        try {
            
            $model = Model('order');
            $model->beginTransaction();
            
            // 第1步 表单验证
            $this->_createOrderStep1();
            
            // 第2步 得到购买商品信息
            $this->_createOrderStep2();
            
            // 第3步 得到购买相关金额计算等信息
            $this->_createOrderStep3();
            
            // 第4步 生成订单
            $this->_createOrderStep4();
            
            // 第5步 处理预存款
            $this->_createOrderStep5();
            $model->commit();
            
            // 第6步 订单后续处理
            $this->_createOrderStep6();
            
            return callback(true, '', $this->_order_data);
        } catch (Exception $e) {
            $model->rollback();
            return callback(false, $e->getMessage());
        }
    }

    /**
     * 删除购物车商品
     *
     * @param unknown $ifcart            
     * @param unknown $cart_ids            
     */
    public function delCart($ifcart, $member_id, $cart_ids)
    {
        if (! $ifcart || ! is_array($cart_ids))
            return;
        $cart_id_str = implode(',', $cart_ids);
        if (preg_match('/^[\d,]+$/', $cart_id_str)) {
            QueueClient::push('delCart', array(
                'buyer_id' => $member_id,
                'cart_ids' => $cart_ids
            ));
        }
    }

    /**
     * 选择不同地区时，异步处理并返回每个店铺总运费以及本地区是否能使用货到付款
     * 如果店铺统一设置了满免运费规则，则运费模板无效
     * 如果店铺未设置满免规则，且使用运费模板，按运费模板计算，如果其中有商品使用相同的运费模板，则两种商品数量相加后再应用该运费模板计算（即作为一种商品算运费）
     * 如果未找到运费模板，按免运费处理
     * 如果没有使用运费模板，商品运费按快递价格计算，运费不随购买数量增加
     */
    public function changeAddr($freight_hash, $city_id, $area_id, $member_id)
    {
        // $city_id计算运费模板,$area_id计算货到付款
        $city_id = intval($city_id);
        $area_id = intval($area_id);
        if ($city_id <= 0 || $area_id <= 0)
            return null;
            
            // 将hash解密，得到运费信息(店铺ID，运费,运费模板ID,购买数量),hash内容有效期为1小时
        $freight_list = $this->buyDecrypt($freight_hash, $member_id);
        // echo json_encode($freight_list);die;
        // 算运费
        $store_freight_list = $this->_logic_buy_1->calcStoreFreight($freight_list, $city_id);
        $data = array();
        $data['state'] = empty($store_freight_list) ? 'fail' : 'success';
        $data['content'] = $store_freight_list;
        
        // 是否能使用货到付款(只有包含平台店铺的商品才会判断)
        // $if_include_platform_store = array_key_exists(DEFAULT_PLATFORM_STORE_ID,$freight_list['iscalced']) || array_key_exists(DEFAULT_PLATFORM_STORE_ID,$freight_list['nocalced']);
        
        // $offline_store_id_array = Model('store')->getOwnShopIds();
        $order_platform_store_ids = array();
        
        if (is_array($freight_list['iscalced']))
            foreach (array_keys($freight_list['iscalced']) as $k)
                // if (in_array($k, $offline_store_id_array))
                $order_platform_store_ids[$k] = null;
        
        if (is_array($freight_list['nocalced']))
            foreach (array_keys($freight_list['nocalced']) as $k)
                // if (in_array($k, $offline_store_id_array))
                $order_platform_store_ids[$k] = null;
            
            // if ($order_platform_store_ids) {
        $allow_offpay_batch = Model('offpay_area')->checkSupportOffpayBatch($area_id, array_keys($order_platform_store_ids));
        /*
         * //JS验证使用
         * $data['allow_offpay'] = array_filter($allow_offpay_batch) ? '1' : '0';
         * $data['allow_offpay_batch'] = $allow_offpay_batch;
         * } else {
         */
        // JS验证使用
        $data['allow_offpay'] = array_filter($allow_offpay_batch) ? '1' : '0';
        $data['allow_offpay_batch'] = $allow_offpay_batch;
        // }
        
        // PHP验证使用
        $data['offpay_hash'] = $this->buyEncrypt($data['allow_offpay'] ? 'allow_offpay' : 'deny_offpay', $member_id);
        $data['offpay_hash_batch'] = $this->buyEncrypt($data['allow_offpay_batch'], $member_id);
        
        return $data;
    }

    /**
     * 验证F码
     *
     * @param int $goods_commonid            
     * @param string $fcode            
     * @return array
     */
    public function checkFcode($goods_commonid, $fcode)
    {
        $fcode_info = Model('goods_fcode')->getGoodsFCode(array(
            'goods_commonid' => $goods_commonid,
            'fc_code' => $fcode,
            'fc_state' => 0
        ));
        if ($fcode_info) {
            return callback(true, '', $fcode_info);
        } else {
            return callback(false, 'F码错误');
        }
    }

    /**
     * 订单生成前的表单验证与处理
     */
    private function _createOrderStep1()
    {
        $post = $this->_post_data;
        
        // 取得商品ID和购买数量
        $input_buy_items = $this->_parseItems($post['gift_id']);
        if (empty($input_buy_items)) {
            throw new Exception('所购商品无效');
        }
        
        if(!$post['is_pick']) {
            //验证收货地址
            $input_address_id = intval($post['address_id']);
            if ($input_address_id <= 0) {
                throw new Exception('请选择收货地址');
            } else {
                $input_address_info = Model('address')->getAddressInfo(array('address_id'=>$input_address_id));
                if ($input_address_info['member_id'] != $this->_member_info['member_id']) {
                    throw new Exception('请选择收货地址');
                }
            }
        
            //收货地址城市编号
            $input_city_id = intval($input_address_info['city_id']);
        } else {
            $setting = Model('setting')->getListSetting();
        
            $input_address_info = array();
            $input_address_info['area_info'] = $setting['delivery_area_info'];
            $input_address_info['address'] = $setting['delivery_address'];
        
            //收货地址城市编号
            $input_city_id = intval($setting['delivery_city_id']);
        }
        
        
        
        //是否开增值税发票
        $input_if_vat = $this->buyDecrypt($post['vat_hash'], $this->_member_info['member_id']);
        if (!in_array($input_if_vat,array('allow_vat','deny_vat'))) {
            throw new Exception('订单保存出现异常[值税发票出现错误]，请重试');
        }
        $input_if_vat = ($input_if_vat == 'allow_vat') ? true : false;
        
        //付款方式:在线支付/货到付款(online/offline)
        if (!in_array($post['pay_name'],array('online','offline'))) {
            throw new Exception('付款方式错误，请重新选择');
        }
        $input_pay_name = $post['pay_name'];
        
        //是否支持货到付款
        if ($input_pay_name=='offline') {
            $input_if_offpay = $this->buyDecrypt($post['offpay_hash'], $this->_member_info['member_id']);
            if (!in_array($input_if_offpay,array('allow_offpay','deny_offpay'))) {
                throw new Exception('订单保存出现异常[货到付款验证错误]，请重试');
            }
        }
        
        $input_if_offpay = ($input_if_offpay == 'allow_offpay') ? true : false;
        // 是否支持货到付款 具体到各个店铺
        if ($input_if_offpay) {
            $input_if_offpay_batch = $this->buyDecrypt($post['offpay_hash_batch'], $this->_member_info['member_id']);
            if (!is_array($input_if_offpay_batch)) {
                throw new Exception('订单保存出现异常[部分店铺付款方式出现异常]，请重试');
            }
        }
        
        //验证发票信息
        if (!empty($post['invoice_id'])) {
            $input_invoice_id = intval($post['invoice_id']);
            if ($input_invoice_id > 0) {
                $input_invoice_info = Model('invoice')->getinvInfo(array('inv_id'=>$input_invoice_id));
                if ($input_invoice_info['member_id'] != $this->_member_info['member_id']) {
                    throw new Exception('请正确填写发票信息');
                }
            }
        }
        
        
        // 保存数据
        $this->_order_data['input_buy_items'] = $input_buy_items;
        $this->_order_data['input_city_id'] = $input_city_id;
        $this->_order_data['input_pay_name'] = $input_pay_name;
        $this->_order_data['input_if_offpay'] = $input_if_offpay;
        $this->_order_data['input_if_offpay_batch'] = $input_if_offpay_batch;
        $this->_order_data['input_pay_message'] = $post['pay_message'];
        $this->_order_data['input_address_info'] = $input_address_info;
        $this->_order_data['input_invoice_info'] = $input_invoice_info;
        $this->_order_data['order_from'] = $post['order_from'] == 2 ? 2 : 1;
    }

    /**
     * 得到购买商品信息
     */
    private function _createOrderStep2()
    {
        $post = $this->_post_data;
        
        $model = Model();
        $con['lottery_gift_id'] = array('in', $post['gift_id']);
        $gift_list = $model->table('lottery_gift')->where($con)->select();;
        
        // [得到最新商品属性及促销信息]
        $gift_list = $this->_logic_buy_1->getGoodsGiftList($gift_list);
        
        // 商品列表 [优惠套装子商品与普通商品同级罗列]
        $goods_list = $this->_getGoodsList($gift_list);
        
        // 以店铺下标归类
        $store_cart_list = $this->_getStoreCartList($gift_list);
        
        //F码验证
        $fc_id = $this->_checkFcode($goods_list, $post['fcode']);
        if(!$fc_id) {
            throw new Exception('F码商品验证错误');
        }
        //保存数据
        $this->_order_data['goods_list'] = $goods_list;
        $this->_order_data['store_cart_list'] = $store_cart_list;
        if ($fc_id > 0) {
            $this->_order_data['fc_id'] = $fc_id;
        }
    }

    /**
     * 得到购买相关金额计算等信息
     */
    private function _createOrderStep3()
    {
        $goods_list = $this->_order_data['goods_list'];
        $store_cart_list = $this->_order_data['store_cart_list'];
        $input_voucher_list = $this->_order_data['input_voucher_list'];
        $input_city_id = $this->_order_data['input_city_id'];
        
        // 商品金额计算(分别对每个商品/优惠套装小计、每个店铺小计)
        list ($store_cart_list, $store_goods_total) = $this->_logic_buy_1->calcCartList($store_cart_list);
        
        // 取得店铺优惠 - 满即送(赠品列表，店铺满送规则列表)
        list ($store_premiums_list, $store_mansong_rule_list) = $this->_logic_buy_1->getMansongRuleCartListByTotal($store_goods_total);
        
        // 重新计算店铺扣除满即送后商品实际支付金额
        $store_final_goods_total = $this->_logic_buy_1->reCalcGoodsTotal($store_goods_total, $store_mansong_rule_list, 'mansong');
        
        // 得到有效的代金券
        $input_voucher_list = $this->_logic_buy_1->reParseVoucherList($input_voucher_list, $store_goods_total, $this->_member_info['member_id']);
        
        // 重新计算店铺扣除优惠券送商品实际支付金额
        $store_final_goods_total = $this->_logic_buy_1->reCalcGoodsTotal($store_final_goods_total, $input_voucher_list, 'voucher');
        
        // 计算每个店铺(所有店铺级优惠活动)总共优惠多少
        $store_promotion_total = $this->_logic_buy_1->getStorePromotionTotal($store_goods_total, $store_final_goods_total);
        
        // 计算每个店铺运费
        list ($need_calc_sid_list, $cancel_calc_sid_list) = $this->_logic_buy_1->getStoreFreightDescList($store_final_goods_total);
        $freight_list = $this->_logic_buy_1->getStoreFreightList($goods_list, array_keys($cancel_calc_sid_list));
        $store_freight_total = $this->_logic_buy_1->calcStoreFreight($freight_list, $input_city_id);
        
        // 计算店铺最终订单实际支付金额(加上运费)
        $store_final_order_total = $this->_logic_buy_1->reCalcGoodsTotal($store_final_goods_total, $store_freight_total, 'freight');
        
        // 计算店铺分类佣金[改由任务计划]
        // zmr>>>
        $store_gc_id_commis_rate_list = Model('store_bind_class')->getStoreGcidCommisRateList($goods_list);
        // zmr<<<
        // 将赠品追加到购买列表(如果库存0，则不送赠品)
        $append_premiums_to_cart_list = $this->_logic_buy_1->appendPremiumsToCartList($store_cart_list, $store_premiums_list, $store_mansong_rule_list, $this->_member_info['member_id']);
        if ($append_premiums_to_cart_list === false) {
            throw new Exception('抱歉，您购买的商品库存不足，请重购买');
        } else {
            list ($store_cart_list, $goods_buy_quantity, $store_mansong_rule_list) = $append_premiums_to_cart_list;
        }
        
        // 保存数据
        $this->_order_data['store_goods_total'] = $store_goods_total;
        $this->_order_data['store_final_order_total'] = $store_final_order_total;
        $this->_order_data['store_freight_total'] = $store_freight_total;
        $this->_order_data['store_promotion_total'] = $store_promotion_total;
        // zmr>>>
        $this->_order_data['store_gc_id_commis_rate_list'] = $store_gc_id_commis_rate_list;
        // zmr<<<
        $this->_order_data['store_mansong_rule_list'] = $store_mansong_rule_list;
        $this->_order_data['store_cart_list'] = $store_cart_list;
        $this->_order_data['goods_buy_quantity'] = $goods_buy_quantity;
        $this->_order_data['input_voucher_list'] = $input_voucher_list;
    }

    /**
     * 生成订单
     *
     * @param array $input            
     * @throws Exception
     * @return array array(支付单sn,订单列表)
     */
    private function _createOrderStep4()
    {
        $is_pick = $this->_post_data['is_pick'];
        extract($this->_order_data);
        
        $member_id = $this->_member_info['member_id'];
        $member_name = $this->_member_info['member_name'];
        $member_email = $this->_member_info['member_email'];
        
        $model_order = Model('order');
        
        // 存储生成的订单数据
        $order_list = array();
        // 存储通知信息
        $notice_list = array();
        
        // 每个店铺订单是货到付款还是线上支付,店铺ID=>付款方式[在线支付/货到付款]
        $store_pay_type_list = $this->_logic_buy_1->getStorePayTypeList(array_keys($store_cart_list), $input_if_offpay, $input_pay_name);
        
        foreach ($store_pay_type_list as $k => & $v) {
            if (empty($input_if_offpay_batch[$k]))
                $v = 'online';
        }
        
        $pay_sn = $this->_logic_buy_1->makePaySn($member_id);
        $order_pay = array();
        $order_pay['pay_sn'] = $pay_sn;
        $order_pay['buyer_id'] = $member_id;
        $total_freight = 0;
        foreach ($store_freight_total as $store_id => $store_freight) {
            $total_freight += $store_freight;
        }
        if($total_freight <= 0) {
            $order_pay['api_pay_state'] = 1;
            $this->_order_data['need_pay'] = 0;
        } else {
            if($is_pick > 0) {
                $order_pay['api_pay_state'] = 1;
                $this->_order_data['need_pay'] = 0;
            } else {
                $this->_order_data['need_pay'] = 1;
            }
        }
        $order_pay_id = $model_order->addOrderPay($order_pay);
        if (! $order_pay_id) {
            throw new Exception('订单保存失败[未生成支付单]');
        }
        
        // 收货人信息
        list ($reciver_info, $reciver_name) = $this->_logic_buy_1->getReciverAddr($input_address_info);
        
        foreach ($store_cart_list as $store_id => $goods_list) {
            
            // 取得本店优惠额度(后面用来计算每件商品实际支付金额，结算需要)
            $promotion_total = ! empty($store_promotion_total[$store_id]) ? $store_promotion_total[$store_id] : 0;
            // 本店总的优惠比例,保留3位小数
            $should_goods_total = $store_final_order_total[$store_id] - $store_freight_total[$store_id] + $promotion_total;
            $promotion_rate = abs(number_format($promotion_total / $should_goods_total, 5));
            if ($promotion_rate <= 1) {
                $promotion_rate = floatval(substr($promotion_rate, 0, 5));
            } else {
                $promotion_rate = 0;
            }
            
            // 每种商品的优惠金额累加保存入 $promotion_sum
            $promotion_sum = 0;
            
            $order = array();
            $order_common = array();
            $order_goods = array();
            
            $order['order_sn'] = $this->_logic_buy_1->makeOrderSn($order_pay_id);
            $order['pay_sn'] = $pay_sn;
            $order['store_id'] = $store_id;
            $order['store_name'] = $goods_list[0]['store_name'];
            $order['buyer_id'] = $member_id;
            $order['buyer_name'] = $member_name;
            $order['buyer_email'] = $member_email;
            $order['add_time'] = TIMESTAMP;
            $order['payment_code'] = $store_pay_type_list[$store_id];
            $order['order_amount'] = $store_final_order_total[$store_id];
            $order['shipping_fee'] = $store_freight_total[$store_id];
            $order['goods_amount'] = $order['order_amount'] - $order['shipping_fee'];
            
            if($is_pick <= 0) {
                $order['order_amount'] = $order['shipping_fee'];
                if($order['order_amount'] > 0) {
                    $order['order_state'] = ORDER_STATE_NEW;
                } else {
                    $order['order_state'] = ORDER_STATE_PAY;
                } 
            } else {
                $order['order_amount'] = 0;
                $order['shipping_fee'] = 0;
                $order['order_state'] = ORDER_STATE_PAY;
            }
            $order['order_from'] = $order_from;
            $order['is_pick'] = $is_pick;
            $order['exchange_state'] = 1;
            // 中山小修改货到付款bug>>>
            if ($order['payment_code'] == "") {
                $order['payment_code'] = "offline";
            }
            
            // 中山小修改货到付款bug<<<
            $order_id = $model_order->addOrder($order);
            
            if (! $order_id) {
                throw new Exception('订单保存失败[未生成订单数据]');
            }
            $order['order_id'] = $order_id;
            $order_list[$order_id] = $order;
            
            $order_common['order_id'] = $order_id;
            $order_common['store_id'] = $store_id;
            $order_common['order_message'] = $input_pay_message[$store_id];
            
            // 代金券
            if (isset($input_voucher_list[$store_id])) {
                $order_common['voucher_price'] = $input_voucher_list[$store_id]['voucher_price'];
                $order_common['voucher_code'] = $input_voucher_list[$store_id]['voucher_code'];
            }
            
            $order_common['reciver_info'] = $reciver_info;
            $order_common['reciver_name'] = $reciver_name;
            $order_common['reciver_city_id'] = $input_city_id;
            
            // 发票信息
            $order_common['invoice_info'] = $this->_logic_buy_1->createInvoiceData($input_invoice_info);
            
            // 保存促销信息
            if (is_array($store_mansong_rule_list[$store_id])) {
                $order_common['promotion_info'] = addslashes($store_mansong_rule_list[$store_id]['desc']);
            }
            
            $order_id = $model_order->addOrderCommon($order_common);
            if (! $order_id) {
                throw new Exception('订单保存失败[未生成订单扩展数据]');
            }
            
            // 生成order_goods订单商品数据
            $i = 0;
            foreach ($goods_list as $goods_info) {
                if (! $goods_info['state'] || ! $goods_info['storage_state']) {
                    throw new Exception('部分商品已经下架或库存不足，请重新选择');
                }
                if (! intval($goods_info['bl_id'])) {
                    // 如果不是优惠套装
                    $order_goods[$i]['order_id'] = $order_id;
                    $order_goods[$i]['goods_id'] = $goods_info['goods_id'];
                    $order_goods[$i]['store_id'] = $store_id;
                    $order_goods[$i]['goods_name'] = $goods_info['goods_name'];
                    $order_goods[$i]['goods_price'] = $goods_info['goods_price'];
                    $order_goods[$i]['goods_num'] = $goods_info['goods_num'];
                    $order_goods[$i]['goods_image'] = $goods_info['goods_image'];
                    $order_goods[$i]['buyer_id'] = $member_id;
                    if ($goods_info['ifgroupbuy']) {
                        $ifgroupbuy = true;
                        $order_goods[$i]['goods_type'] = 2;
                    } elseif ($goods_info['ifxianshi']) {
                        $order_goods[$i]['goods_type'] = 3;
                    } elseif ($goods_info['ifzengpin']) {
                        $order_goods[$i]['goods_type'] = 5;
                    } else {
                        $order_goods[$i]['goods_type'] = 1;
                    }
                    $order_goods[$i]['promotions_id'] = $goods_info['promotions_id'] ? $goods_info['promotions_id'] : 0;
                    // zmr>>>
                    $order_goods[$i]['commis_rate'] = floatval($store_gc_id_commis_rate_list[$store_id][$goods_info['gc_id']]);
                    // zmr<<<
                    $order_goods[$i]['gc_id'] = $goods_info['gc_id'];
                    // 计算商品金额
                    $goods_total = $goods_info['goods_price'] * $goods_info['goods_num'];
                    // 计算本件商品优惠金额
                    $promotion_value = floor($goods_total * ($promotion_rate));
                    $order_goods[$i]['goods_pay_price'] = $goods_total - $promotion_value;
                    $promotion_sum += $promotion_value;
                    $i ++;
                    
                    // 存储库存报警数据
                    if ($goods_info['goods_storage_alarm'] >= ($goods_info['goods_storage'] - $goods_info['goods_num'])) {
                        $param = array();
                        $param['common_id'] = $goods_info['goods_commonid'];
                        $param['sku_id'] = $goods_info['goods_id'];
                        $notice_list['goods_storage_alarm'][$goods_info['store_id']] = $param;
                    }
                } elseif (! empty($goods_info['bl_goods_list']) && is_array($goods_info['bl_goods_list'])) {
                    
                    // 优惠套装
                    foreach ($goods_info['bl_goods_list'] as $bl_goods_info) {
                        $order_goods[$i]['order_id'] = $order_id;
                        $order_goods[$i]['goods_id'] = $bl_goods_info['goods_id'];
                        $order_goods[$i]['store_id'] = $store_id;
                        $order_goods[$i]['goods_name'] = $bl_goods_info['goods_name'];
                        $order_goods[$i]['goods_price'] = $bl_goods_info['bl_goods_price'];
                        $order_goods[$i]['goods_num'] = $goods_info['goods_num'];
                        $order_goods[$i]['goods_image'] = $bl_goods_info['goods_image'];
                        $order_goods[$i]['buyer_id'] = $member_id;
                        $order_goods[$i]['goods_type'] = 4;
                        $order_goods[$i]['promotions_id'] = $bl_goods_info['bl_id'];
                        $order_goods[$i]['commis_rate'] = floatval($store_gc_id_commis_rate_list[$store_id][$goods_info['gc_id']]);
                        $order_goods[$i]['gc_id'] = $bl_goods_info['gc_id'];
                        
                        // 计算商品实际支付金额(goods_price减去分摊优惠金额后的值)
                        $goods_total = $bl_goods_info['bl_goods_price'] * $goods_info['goods_num'];
                        // 计算本件商品优惠金额
                        $promotion_value = floor($goods_total * ($promotion_rate));
                        $order_goods[$i]['goods_pay_price'] = $goods_total - $promotion_value;
                        $promotion_sum += $promotion_value;
                        $i ++;
                        
                        // 存储库存报警数据
                        if ($bl_goods_info['goods_storage_alarm'] >= ($bl_goods_info['goods_storage'] - $goods_info['goods_num'])) {
                            $param = array();
                            $param['common_id'] = $bl_goods_info['goods_commonid'];
                            $param['sku_id'] = $bl_goods_info['goods_id'];
                            $notice_list['goods_storage_alarm'][$bl_goods_info['store_id']] = $param;
                        }
                    }
                }
            }
            
            // 将因舍出小数部分出现的差值补到最后一个商品的实际成交价中(商品goods_price=0时不给补，可能是赠品)
            if ($promotion_total > $promotion_sum) {
                $i --;
                for ($i; $i >= 0; $i --) {
                    if (floatval($order_goods[$i]['goods_price']) > 0) {
                        $order_goods[$i]['goods_pay_price'] -= $promotion_total - $promotion_sum;
                        break;
                    }
                }
            }
            $insert = $model_order->addOrderGoods($order_goods);
            if (! $insert) {
                throw new Exception('订单保存失败[未生成商品数据]');
            }
            
            // 存储商家发货提醒数据
            if ($store_pay_type_list[$store_id] == 'offline') {
                $notice_list['new_order'][$order['store_id']] = array(
                    'order_sn' => $order['order_sn']
                );
            }
        }
        
        // 保存数据
        $this->_order_data['pay_sn'] = $pay_sn;
        $this->_order_data['order_list'] = $order_list;
        $this->_order_data['notice_list'] = $notice_list;
        $this->_order_data['ifgroupbuy'] = $ifgroupbuy;
    }

    /**
     * 充值卡、预存款支付
     */
    private function _createOrderStep5()
    {
        if (empty($this->_post_data['password']))
            return;
        $buyer_info = Model('member')->getMemberInfoByID($this->_member_info['member_id']);
        if ($buyer_info['member_paypwd'] == '' || $buyer_info['member_paypwd'] != md5($this->_post_data['password']))
            return;
    }

    /**
     * 订单后续其它处理
     */
    private function _createOrderStep6()
    {
        $goods_buy_quantity = $this->_order_data['goods_buy_quantity'];
        $store_cart_list = $this->_order_data['store_cart_list'];
        $input_buy_items = $this->_order_data['input_buy_items'];
        $order_list = $this->_order_data['order_list'];
        $input_address_info = $this->_order_data['input_address_info'];
        $notice_list = $this->_order_data['notice_list'];
        $ifgroupbuy = $this->_order_data['ifgroupbuy'];
        
        // 变更库存和销量
        QueueClient::push('createOrderUpdateStorage', $goods_buy_quantity);
        
        // 保存订单自提点信息
        if (C('delivery_isuse') && intval($input_address_info['dlyp_id'])) {
            $data = array();
            $data['mob_phone'] = $input_address_info['mob_phone'];
            $data['tel_phone'] = $input_address_info['tel_phone'];
            $data['reciver_name'] = $input_address_info['true_name'];
            $data['dlyp_id'] = $input_address_info['dlyp_id'];
            foreach ($order_list as $v) {
                $data['order_sn_list'][$v['order_id']]['order_sn'] = $v['order_sn'];
                $data['order_sn_list'][$v['order_id']]['add_time'] = $v['add_time'];
            }
            QueueClient::push('saveDeliveryOrder', $data);
        }
        
        // 发送提醒类信息
        if (! empty($notice_list)) {
            foreach ($notice_list as $code => $value) {
                QueueClient::push('sendStoreMsg', 
                    array(
                        'code' => $code,
                        'store_id' => key($value),
                        'param' => current($value)
                    ));
            }
        }
    }

    /**
     * 加密
     *
     * @param array/string $string            
     * @param int $member_id            
     * @return mixed arrray/string
     */
    public function buyEncrypt($string, $member_id)
    {
        $buy_key = sha1(md5($member_id . '&' . MD5_KEY));
        if (is_array($string)) {
            $string = serialize($string);
        } else {
            $string = strval($string);
        }
        return encrypt(base64_encode($string), $buy_key);
    }

    /**
     * 解密
     *
     * @param string $string            
     * @param int $member_id            
     * @param number $ttl            
     */
    public function buyDecrypt($string, $member_id, $ttl = 0)
    {
        $buy_key = sha1(md5($member_id . '&' . MD5_KEY));
        if (empty($string))
            return;
        $string = base64_decode(decrypt(strval($string), $buy_key, $ttl));
        return ($tmp = @unserialize($string)) !== false ? $tmp : $string;
    }

    /**
     * 得到所购买的id和数量
     */
    private function _parseItems($gift_id)
    {
        $model_gift = Model('lottery_gift');
        $con['lottery_gift_id'] = array('in', $gift_id);
        $gifts = $model_gift->getGiftExtendList($con);
        
        // 存放所购商品ID和数量组成的键值对
        $buy_items = array();
        if (is_array($gifts)) {
            foreach ($gifts as $value) {
                $buy_items[$value['goods_id']] = 1;
            }
        }
        return $buy_items;
    }

    /**
     * 从购物车数组中得到商品列表
     *
     * @param unknown $cart_list            
     */
    private function _getGoodsList($gift_list)
    {
        if (empty($gift_list) || ! is_array($gift_list))
            return $gift_list;
        $goods_list = array();
        $i = 0;
        foreach ($gift_list as $key => $gift) {
//             if (! $gift['state'] || ! $gift['storage_state'])
//                 continue;
            
            $goods_list[$i]['goods_num'] = $gift['goods_num'];
            $goods_list[$i]['goods_id'] = $gift['goods_id'];
            $goods_list[$i]['store_id'] = $gift['store_id'];
            $goods_list[$i]['gc_id'] = $gift['gc_id'];
            $goods_list[$i]['goods_name'] = $gift['goods_name'];
            $goods_list[$i]['goods_price'] = $gift['goods_price'];
            $goods_list[$i]['store_name'] = $gift['store_name'];
            $goods_list[$i]['goods_image'] = $gift['goods_image'];
            $goods_list[$i]['transport_id'] = $gift['transport_id'];
            $goods_list[$i]['goods_freight'] = $gift['goods_freight'];
            $goods_list[$i]['goods_vat'] = $gift['goods_vat'];
            $goods_list[$i]['is_fcode'] = $gift['is_fcode'];
            $goods_list[$i]['is_pick'] = $gift['is_pick'];
            $i ++;
        }
        return $goods_list;
    }

    /**
     * 将下单商品列表转换为以店铺ID为下标的数组
     *
     * @param array $cart_list            
     * @return array
     */
    private function _getStoreCartList($gift_list)
    {
        if (empty($gift_list) || ! is_array($gift_list))
            return $gift_list;
        $new_array = array();
        foreach ($gift_list as $gift) {
            $new_array[$gift['store_id']][] = $gift;
        }
        return $new_array;
    }

    /**
     * 本次下单是否需要码及F码合法性
     * 无需使用F码，返回 true
     * 需要使用F码，返回($fc_id/false)
     */
    private function _checkFcode($goods_list, $fcode)
    {
        foreach ($goods_list as $k => $v) {
            if ($v['is_fcode'] == 1) {
                $is_fcode = true;
                break;
            }
        }
        if (! $is_fcode)
            return true;
        if (empty($fcode) || count($goods_list) > 1) {
            return false;
        }
        $goods_info = $goods_list[0];
        $fcode_info = $this->checkFcode($goods_info['goods_commonid'], $fcode);
        if ($fcode_info['state']) {
            return intval($fcode_info['data']['fc_id']);
        } else {
            return false;
        }
    }

    function changeyunfei($member_id, $goods)
    {
        
        // 输出用户默认收货地址
        $address_info = Model('address')->getDefaultAddressInfo(array(
            'member_id' => $member_id
        ));
        
        // 按照默认地址计算运费 陈雨楠 2016-9-22
        // 收货地址城市编号
        $input_city_id = intval($address_info['city_id']);
        // echo json_encode($goods['transport_id']);die;
        $model_transport = Model('transport');
        $freight_total = $model_transport->calc_transport($goods['transport_id'], $goods['goods_num'], $input_city_id);
        return $freight_total;
        // echo json_encode($store_freight_total);die;
        // 按照默认地址计算运费 陈雨楠 2016-9-22 end
    }
}
